//
// Created by 谢哲 on 2021/5/20.
//

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main() {
    int n, k;
    cin >> n >> k;

    vector<int> vdata(n);
    for (auto &p: vdata) cin >> p;
    sort(vdata.begin(), vdata.end());

    vector<int> ldata(n, 0), rdata(n, 0);
    for (int i=0, j=0; i<n; ++i) {
        for (;j<n&&vdata[j]-vdata[i]<=k;++j) {
            rdata[j] = j - i + 1;
        }
        ldata[i] = j - i;
    }

    int ans = 0;
    int max_rans = 0;
    for (int i=0; i<n; ++i) {
        int rans = i>0 ? rdata[i-1] : 0;
        max_rans = max(rans, max_rans);
        ans = max(ans, max_rans + ldata[i]);
    }

    cout << ans;

    return 0;
}